package logic;

import java.io.Serializable;

/**
 * 
 * @author Marco Gallardo Casu
 *
 */
public class Square implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9085283962748988424L;

	public static final int LENGTH = 3;
	private int[][] square;

	public Square() {
		square = new int[LENGTH][LENGTH];
	}

	public Square(int[] genes) {
		this();
		for (int row = 0; row < LENGTH; row++) {
			for (int col = 0; col < LENGTH; col++) {
				square[row][col] = genes[row * LENGTH + col];
			}
		}
	}

	private Square(int[][] square) {
		this();
		for (int index = 0; index < LENGTH; index++) {
			this.square[index] = square[index].clone();
		}
	}

	public int[][] getSquare() {
		return square;
	}

	public int getBox(int row, int col) {
		return square[row][col];
	}

	public void setSquare(int[][] square) {
		this.square = square;
	}

	public void setBox(int value, int row, int col) {
		this.square[row][col] = value;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return new Square(this.square);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Square)) {
			return false;
		} else if (this == obj) {
			return true;
		} else {
			Square s = (Square) obj;
			boolean equals = true;
			for (int row = 0; equals && row < LENGTH; row++) {
				for (int col = 0; equals && row < LENGTH; col++)
				equals = s.getBox(row, col) == getBox(row, col);
			}
			return equals;
		}
	}

}
